package cn.ibobo.lease.admin.service.impl;

import cn.ibobo.lease.admin.dao.UserCouponMapper;
import cn.ibobo.lease.admin.dao.UserMapper;
import cn.ibobo.lease.common.base.BaseMapper;
import cn.ibobo.lease.common.base.BaseServiceImpl;
import cn.ibobo.lease.common.dto.UserCouponDTO;
import cn.ibobo.lease.admin.service.UserCouponService;
import cn.ibobo.lease.common.dto.UserDTO;
import cn.ibobo.lease.common.enums.Pagination;
import cn.ibobo.lease.common.enums.ResultEnum;
import cn.ibobo.lease.common.util.DateUtil;
import cn.ibobo.lease.common.util.UuidUtil;
import com.github.pagehelper.PageHelper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;
import java.util.List;


/**
 * 用户点金券领用表业务处理接口
 * @Author lililiang
 * @Date 2018-12-13 18:36:40
 */
@Service
public class UserCouponServiceImpl extends BaseServiceImpl<UserCouponDTO, String> implements UserCouponService {

    private static final Log LOGGER = LogFactory.getLog(UserCouponServiceImpl.class);

    @Autowired
    private UserCouponMapper mapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    protected BaseMapper<UserCouponDTO, String> getMapper() {
        return mapper;
    }

    /**
     * 用户点金券领用表列表
     * @Author lililiang
     * @Date 2018-12-13 18:36:40
     * @Param
     * @Return
     */
    @Override
    public List<UserCouponDTO> list4Page(UserCouponDTO entity, Integer pageNum, Integer... pageSize) {
        return super.list4Page(entity, pageNum, pageSize);
    }

    /**
     * 用户点金券领用表列表（无分页）
     * @Author lililiang
     * @Date 2018-12-13 18:36:40
     * @Param
     * @Return
     */
    @Override
    public List<UserCouponDTO> listByCondition(UserCouponDTO entity) {
        return super.listByCondition(entity);
    }

    /**
     * 查询用户点金券领用表详情信息
     * @Author lililiang
     * @Date 2018-12-13 18:36:40
     * @Param
     * @Return
     */
    @Override
    public UserCouponDTO queryOneByPk(String id) {
        return super.queryOneByPk(id);
    }

    /**
     * 保存用户点金券领用表信息
     * @Author lililiang
     * @Date 2018-12-13 18:36:40
     * @Param
     * @Return
     */
    @Override
    @Transactional
    public Integer save(UserCouponDTO entity) {
        String id = entity.getId();
        if(id == null || id.length() == 0){
            entity.setId(UuidUtil.getUuid());
            return mapper.add(entity);
        }else{
            return mapper.edit(entity);
        }
    }

    /**
     * 删除用户点金券领用表信息
     * @Author lililiang
     * @Date 2018-12-13 18:36:40
     * @Param
     * @Return
     */
    @Override
    @Transactional
    public Integer delete(String id) {
        return super.delete(id);
    }

    /**
     * @Description 增加减少点金券
     * @Author lililiang
     * @Date 2019/2/16 15:20
     * @Param [entityDTO]
     * @Return java.lang.Integer
     */
    @Override
    @Transactional
    public Integer coupon(UserCouponDTO entityDTO) {
        //1. 用户点金账户减去使用点金金额
        UserDTO user = new UserDTO();
        user.setUid(entityDTO.getUid());
        user.setCoupon(entityDTO.getMoney());
        if(entityDTO.getType() == 6){//6线下购物使用
            user.setType((byte)2);//减少
        }else{
            user.setType((byte)1);
        }
        if(userMapper.coupon(user) <= 0){
            return ResultEnum.NO_ENOUGH_COUPON.getCode();
        }
        //2. 增加点金记录
        entityDTO.setId(UuidUtil.getUuid());
        entityDTO.setCreatetm(DateUtil.getNow());
        return mapper.add(entityDTO);
    }


    /**
     * @Description  商家点金券统计
     * @Author lililiang
     * @Date 2019/3/11 16:36
     * @Param [entityDTO, pageNum, pageSize]
     * @Return java.util.List<cn.ibobo.lease.common.dto.UserCouponDTO>
     */
    @Override
    @Transactional
    public List<UserCouponDTO> listBus4Page(UserCouponDTO entity, Integer pageNum, Integer... pageSize){
        Integer curPageSize = 0;
        for (Integer ps : pageSize) {
            curPageSize = ps;
        }
        PageHelper.startPage(pageNum, curPageSize != null && curPageSize > 0 ? curPageSize : Pagination.PAGE_SIZE.getVal());
        return mapper.listBus4Page(entity);
    }
}
